//
//  TTACCView.m
//  TaoTuo
//
//  Created by admin on 2019/8/21.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTACCView.h"
#import "TTFiltrateView.h"
#import "TTScopeView.h"
#import "LLTagView.h"
#import "LLTagConfig.h"
#import "TTSearchInsertModel.h"
#import "TTSearchModel.h"
#import "TTSearchViewController.h"

@interface TTACCView ()
{
    CGFloat viewH;
    CGFloat tagViewH;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, strong) TTScopeView *lbView; // 类别
@property (nonatomic, strong) TTFiltrateView *lbFilteView;
@property (nonatomic, strong) TTScopeView *cpView;  // 产品
@property (nonatomic, strong) TTFiltrateView *cpFilteView;
@property (nonatomic, strong) TTFiltrateView *xkFilteView; // 镶口
@property (nonatomic, strong) UILabel *luoshiSizeLabel;     // 裸石大小
@property (nonatomic, strong) TTFiltrateView *szFilteView; // 手镯
@property (nonatomic, strong) UILabel *szDiameterLabel; // 手掌直径
@property (nonatomic, strong) TTFiltrateView *jzFilteView; // 金重
@property (nonatomic, strong) TTScopeView *czView; // 材质
@property (nonatomic, strong) TTFiltrateView *czFilteView;
@property (nonatomic, strong) TTScopeView *gyView; // 工艺
@property (nonatomic, strong) TTFiltrateView *gyTopFilteView;
@property (nonatomic, strong) TTFiltrateView *gyBottomFilteView;
@property (nonatomic, strong) TTScopeView *jbView; // 戒臂
@property (nonatomic, strong) TTFiltrateView *jbFilteView;
@property (nonatomic, strong) TTScopeView *fsView; // 副石
@property (nonatomic, strong) TTFiltrateView *fsFilteView;
@property (nonatomic, strong) TTScopeView *hotView;
@property (nonatomic, strong) UIButton *moreBtn;

@property (nonatomic, strong) LLTagView *tagView;     // 热门搜索

@property (nonatomic, assign) BOOL isSz;    // 选中了手镯

@property (nonatomic, strong) NSArray *filteViewsArray;
@property (nonatomic, strong) NSArray *scopeViewArray;

@end


@implementation TTACCView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.searchModel = [[TTSearchModel alloc] init];
        self.searchModel.sourceType = @(2);
        
        _searchNameDic = [NSMutableDictionary dictionary];
        
        viewH = 40;
        
    }
    return self;
}

- (void)resetView
{
    self.searchModel = nil;
    self.searchModel = [[TTSearchModel alloc] init];
    self.searchModel.sourceType = @(2);
    
    
    for (TTScopeView *sv in self.scopeViewArray) {
        [sv resetView];
    }
    for (TTFiltrateView *fv in self.filteViewsArray) {
        [fv resetView];
    }
    [self.lbFilteView setSelectedFirstItem];
    [self.tagView resetView];
    
    self.gyBottomFilteView.dataArray = @[];
    
    [self.searchNameDic removeAllObjects];
    [self.searchNameDic setObject:@"女戒" forKey:@"1"];
    
    [self.cpFilteView defaultSelectBeforeIndex:1];    // 默认选中前两个
    
    [self setNeedsLayout];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.szView.hidden = !self.isSz;
    self.szFilteView.hidden = !self.isSz;
    self.szDiameterLabel.hidden = self.szFilteView.hidden;
    
    CGFloat jzTop = self.isSz ? self.szFilteView.bottom : self.xkFilteView.bottom;
    self.jzView.frame = CGRectMake(0, jzTop, self.width, viewH);
    self.jzFilteView.frame = CGRectMake(0, _jzView.bottom, self.width, viewH);
    
    self.czView.frame = CGRectMake(0, _jzFilteView.bottom, self.width, viewH);
    self.czFilteView.frame = CGRectMake(0, _czView.bottom, self.width, viewH);
    
    self.gyView.frame = CGRectMake(0, _czFilteView.bottom, self.width, viewH);
    self.gyTopFilteView.frame = CGRectMake(0, _gyView.bottom, self.width, viewH);
    self.gyBottomFilteView.frame = CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH);
    
    CGFloat top = self.gyBottomFilteView.dataArray.count == 0 ? _gyTopFilteView.bottom : _gyBottomFilteView.bottom;
    self.jbView.frame = CGRectMake(0, top, self.width, viewH);
    self.jbFilteView.frame = CGRectMake(0, _jbView.bottom, self.width, viewH);
    
    self.fsView.frame = CGRectMake(0, _jbFilteView.bottom, self.width, viewH);
    self.fsFilteView.frame = CGRectMake(0, _fsView.bottom, self.width, viewH);
    
    self.hotView.frame = CGRectMake(0, _fsFilteView.bottom, self.width, viewH);
    self.moreBtn.frame = CGRectMake(self.width - 100, _hotView.top, 90, viewH);
    
    CGFloat h = 90;
    if (self.moreBtn.selected) {
        h = tagViewH;
    }
    self.tagView.frame = CGRectMake(0, _hotView.bottom, self.width, h);
    
    
}

- (void)topSearchBtnClick
{
    if (self.clickSearchBlock) {
        self.clickSearchBlock();
    }
}

- (void)setXkDataArray:(NSArray *)xkArray szDataArray:(NSArray *)szArray jzDataArray:(NSArray *)jzArray hotSearchArray:(NSArray *)hotSearchArray
{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:_scrollView];
    
    _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _topBtn.frame = CGRectMake(38, 10, self.width - 38 * 2, 30);
    _topBtn.backgroundColor = UIColorHex(F0F4F7);
    [_topBtn setImage:[UIImage imageNamed:@"good_list_search"] forState:UIControlStateNormal];
    [_topBtn setTitle:@"请输入款号/款式名称进行搜索" forState:UIControlStateNormal];
    [_topBtn setTitleColor:UIColorHex(999999) forState:UIControlStateNormal];
    _topBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    _topBtn.layer.cornerRadius = 15;
    [_topBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _topBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    _topBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [_topBtn addTarget:self action:@selector(topSearchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:_topBtn];
    
    _lbView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _topBtn.bottom, self.width, viewH - 8) title:@"类别" hiddenTextField:YES];
    [self.scrollView addSubview:_lbView];
    @weakify(self)
    _lbFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _lbView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.categoryName = selectedArray.firstObject;
        self.isSz = [selectedArray.firstObject isEqualToString:@"手镯"];
        [self setSearchNameDicWithKey:@"0" value:selectedArray.firstObject];
        [self setNeedsLayout];
    } leftRightSpace:0];
    _lbFilteView.dataArray = @[@"吊坠", @"项链", @"套链", @"手链", @"手镯", @"耳钉", @"耳环", @"耳线", @"耳坠"];
    [_lbFilteView setSelectedFirstItem];    // 默认选中第一个
    [self.scrollView addSubview:_lbFilteView];
    
    _cpView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _lbFilteView.bottom, self.width, viewH - 8) title:@"产品" hiddenTextField:YES];
    [self.scrollView addSubview:_cpView];
    
    _cpFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _cpView.bottom, self.width, viewH) allowsMultipleButAtLeastOne:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        NSNumber *finishedProduct = @0;
        if (selectedArray.count < 2) {
            finishedProduct = [@"空托" isEqualToString:selectedArray.firstObject] ? @1 : @2;
        }
        self.searchModel.finishedProduct = finishedProduct;
        [self setSearchNameDicWithKey:@"1" value:[selectedArray componentsJoinedByString:@" "]];
    } leftRightSpace:0];
    _cpFilteView.dataArray = @[@"空托", @"成品"];
    [_cpFilteView defaultSelectBeforeIndex:1];    // 默认选中前两个
    [self.scrollView addSubview:_cpFilteView];
    
    _xkView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _cpFilteView.bottom, self.width, viewH) title:@"镶口" hiddenTextField:NO];
    [self.scrollView addSubview:_xkView];
    _xkFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _xkView.bottom + 6, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.xkView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@分",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"2" value:str];
    } leftRightSpace:0];
    _xkFilteView.dataArray = xkArray;
    [self.scrollView addSubview:_xkFilteView];
    
    _luoshiSizeLabel = [UILabel labelWithTitle:@"裸石大小" color:UIColorHex(666666) fontSize:8];
    _luoshiSizeLabel.frame = CGRectMake(10, _xkFilteView.top - 8, 100, 10);
    [self.scrollView addSubview:_luoshiSizeLabel];
    
    _szView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _xkFilteView.bottom, self.width, viewH) title:@"手镯内径" hiddenTextField:NO];
    _szView.hidden = YES;
    [self.scrollView addSubview:_szView];
    _szFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _szView.bottom + 6, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.szView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"3" value:str];
    } leftRightSpace:0];
    _szFilteView.dataArray = szArray;
    _szFilteView.hidden = YES;
    [self.scrollView addSubview:_szFilteView];
    _szDiameterLabel = [UILabel labelWithTitle:@"手掌直径" color:UIColorHex(666666) fontSize:8];
    _szDiameterLabel.hidden = YES;
    _szDiameterLabel.frame = CGRectMake(10, _szFilteView.top - 8, 100, 10);
    [self.scrollView addSubview:_szDiameterLabel];
    
    _jzView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _szFilteView.bottom, self.width, viewH) title:@"金重" hiddenTextField:NO];
    [self.scrollView addSubview:_jzView];
    _jzFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _jzView.bottom, self.width, viewH) allowsMultiple:YES selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        TTSearchInsertModel *minModel = selectedArray.firstObject;
        TTSearchInsertModel *maxModel = selectedArray.lastObject;
        [self.jzView setMinStr:minModel.min maxStr:maxModel.max];
        NSString *str = @"";
        if (maxModel) {
            str = [NSString stringWithFormat:@"%@-%@g",minModel.min, maxModel.max];
        }
        [self setSearchNameDicWithKey:@"4" value:str];
    } leftRightSpace:0];
    _jzFilteView.dataArray = jzArray;
    [self.scrollView addSubview:_jzFilteView];
    
    
    _czView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _jzFilteView.bottom, self.width, viewH - 8) title:@"材质" hiddenTextField:YES];
    [self.scrollView addSubview:_czView];
    _czFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _czView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.material = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"5" value:selectedArray.firstObject];
    } leftRightSpace:0];
    _czFilteView.dataArray = @[@"AU750", @"PT950"];
    [self.scrollView addSubview:_czFilteView];
    
    NSDictionary *dic = @{
                          @"爪镶" : @[@"3-4爪", @"5-6爪", @"爪镶其他"],
                          @"包夹镶" : @[@"2-3叶包", @"4-6叶包", @"包夹其他"],
                          @"群镶" : @[]
                          };
    _gyView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _czFilteView.bottom, self.width, viewH - 8) title:@"镶法" hiddenTextField:YES];
    [self.scrollView addSubview:_gyView];
    _gyTopFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _gyView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.process = selectedArray.firstObject;
        self.gyBottomFilteView.dataArray = dic[selectedArray.firstObject];
        [self setSearchNameDicWithKey:@"6" value:selectedArray.firstObject];
        [self setNeedsLayout];
    } leftRightSpace:0];
    _gyTopFilteView.dataArray = @[@"爪镶", @"包夹镶", @"群镶"];
    [self.scrollView addSubview:_gyTopFilteView];
    _gyBottomFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.childProcess = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"7" value:selectedArray.firstObject];
    } leftRightSpace:0];
    [self.scrollView addSubview:_gyBottomFilteView];
    
    
    _jbView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _gyTopFilteView.bottom, self.width, viewH - 8) title:@"戒臂" hiddenTextField:YES];
    [self.scrollView addSubview:_jbView];
    _jbFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _jbView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        self.searchModel.ringArms = selectedArray.firstObject;
        [self setSearchNameDicWithKey:@"8" value:selectedArray.firstObject];
    } leftRightSpace:0];
    _jbFilteView.dataArray = @[@"直臂", @"扭臂"];
    [self.scrollView addSubview:_jbFilteView];
    
    
    _fsView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _jbFilteView.bottom, self.width, viewH - 8) title:@"副石" hiddenTextField:YES];
    [self.scrollView addSubview:_fsView];
    _fsFilteView = [[TTFiltrateView alloc] initWithFrame:CGRectMake(0, _fsView.bottom, self.width, viewH) allowsMultiple:NO selectedArrayBlock:^(NSArray * _Nullable selectedArray) {
        @strongify(self)
        NSString *has = [selectedArray.firstObject isEqualToString:@"有"] ? @"true" : @"false";
        self.searchModel.isExistVStone = has;
        NSString *str = [selectedArray.firstObject isEqualToString:@"有"] ? @"副石" : @"";
        [self setSearchNameDicWithKey:@"9" value:str];
    } leftRightSpace:0];
    _fsFilteView.dataArray = @[@"有", @"无"];
    [self.scrollView addSubview:_fsFilteView];
    
    _hotView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, _fsFilteView.bottom, self.width, viewH) title:@"热门搜索" hiddenTextField:YES];
    [self.scrollView addSubview:_hotView];
    _moreBtn = [UIButton createButtonWithNormalTitle:@"更多" selectedTitle:@"收起" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:12] target:self action:@selector(moreBtnClick:)];
    _moreBtn.frame = CGRectMake(self.width - 100, _hotView.top, 90, viewH);
    [_moreBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.scrollView addSubview:_moreBtn];
    
    NSMutableArray *hotArray = [NSMutableArray array];
    for (TTSearchInsertModel *model in hotSearchArray) {
        [hotArray addObject:model.title];
    }
    LLTagConfig *config = [[LLTagConfig alloc] init];
    config.minItemSpace = 5;
    config.minLineSpace = 5;
    config.sectionInset = UIEdgeInsetsZero;
    config.itemHeight = viewH;
    config.tagNormalbackgroundColor = UIColorHex(F0F4F7);
    config.tagSelectbackgroundColor = kMainColor;
    
    _tagView = [[LLTagView alloc] initWithFrame:CGRectMake(0, _hotView.bottom, self.width, 90) dataArray:hotArray selectItems:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.searchKey = selectArray.firstObject;
    }];
    _tagView.canCancelSelect = YES;
    _tagView.backgroundColor = [UIColor whiteColor];
    _tagView.config = config;
    _tagView.allowMutiSelect = NO;
    _tagView.layer.masksToBounds = YES;
    [self.scrollView addSubview:_tagView];
    
    
    self.filteViewsArray = @[_lbFilteView, _cpFilteView, _xkFilteView, _szFilteView, _jzFilteView, _czFilteView, _gyTopFilteView, _gyBottomFilteView, _jbFilteView, _fsFilteView];
    self.scopeViewArray = @[_xkView, _szView, _jzView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self->tagViewH = self.tagView.height;
        
        self.tagView.height = 90;
        
        [self setScrollViewContentSize];
        
    });
    
    
}




- (void)setScrollViewContentSize
{
    self.scrollView.contentSize = CGSizeMake(self.width, self.filteViewsArray.count * 2 * self->viewH + self.tagView.height + 30);
}

- (void)moreBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    self.tagView.height = sender.selected ? tagViewH : 90;
    
    [self setScrollViewContentSize];
}

- (void)setSearchNameDicWithKey:(NSString *)key value:(NSString *)value
{
    NSString *str = value.length > 0 ? value : @"";
    [self.searchNameDic setObject:str forKey:key];
}


#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView
{
    return self;
}


@end
